package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ciwong.epaper.util.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 自定义城市选择view
 * @author: wyk
 * @time: 2021/8/4 17:42
 */
public class LetterListView extends View {
    OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    private List<String> PinYinList = new ArrayList<>();
    int choose = -1;
    Paint paint = new Paint();
    boolean showBkg = false;
    private Context mContext;

    public LetterListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public LetterListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LetterListView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        paint.setColor(Color.parseColor("#00B395"));
        paint.setTextSize(DensityUtils.sp2px(mContext, 12));
        paint.setAntiAlias(true);
    }
    public void setNewData(List<String> pinYinList){
        this.PinYinList = pinYinList;
        invalidate();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showBkg) {
            canvas.drawColor(Color.parseColor("#40000000"));
        }
        int height = getHeight();
        int width = getWidth();
        int singleHeight = height;
        if (PinYinList.size()>0){
             singleHeight = height / PinYinList.size();
        }

        for (int i = 0; i < PinYinList.size(); i++) {
            float xPos = width / 2 - paint.measureText(PinYinList.get(i)) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(PinYinList.get(i), xPos, yPos, paint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / getHeight() * PinYinList.size());
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < PinYinList.size()) {
                        listener.onTouchingLetterChanged(PinYinList.get(c));
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < PinYinList.size()) {
                        listener.onTouchingLetterChanged(PinYinList.get(c));
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                showBkg = false;
                choose = -1;
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }

}
